<?php
class Dbmysqli{
    protected   $sql    =   '';
    protected   $table  =   '';
    protected   $data   =   '';
    protected   $where  =   '';
    protected   $order  =   '';
    protected   $limit  =   '';
    protected   $mysqli;
    public function __construct($tableName){
        $this->_table   =   DB_PRE.$tableName;
        $this->connect(); //connect mysql
    }
    public function connect(){
        $this->_mysqli =   new mysqli(DB_HOST,DB_USER,DB_PWD,DB_NAME);
        if(mysqli_connect_errno()){
            echo mysqli_connect_error();
        }
     /*   $this->_mysqli =   mysqli_init();
        $this->_mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT,2);//timeout
        $this->_mysqli->real_connect(DB_HOST,DB_USER,DB_PW,DB_NAME);*/
    }
    public function create($data){
        $sql = "CREATE TABLE users 
            (
            ID int NOT NULL AUTO_INCREMENT, 
            PRIMARY KEY(ID),
            Name varchar(15),
            Pwd varchar(15),
            Age int
            )";
            echo $sql;
        $this->_mysqli->query($sql);
   }
    public function find(){
        $sql    =   "select * from ".$this->_table;
        $result   =   $this->mysqli_query($sql);
        if($result ==false){
            return false;
        }
        $field_arr  =   $result->fetch_fields();
        $data   =   array();
        $result->data_seek(0);
        while($row  =   $result->fetch_assoc()){
            $data[] =   $row;
        }
        return $data;
    }
    public function add($data){
        $sql    =   "insert into ".$this->_table;
        $keys   =   '(';
        $values =   '(';
       foreach($data as $key  =>  $v){
            $keys.="".$key.",";
            if(is_numeric($v)){
                $values .=   $v.",";
            }else{
                $values .=   "'".$v."',";
            }
        }
        $keys   =   substr($keys,0,strlen($keys)-1);
        $values   =   substr($values,0,strlen($values)-1);
        $sql    .=$keys.") VALUES ".$values.");";
        $this->mysqli_query($sql);
        $queryId    =   $this->_mysqli->insert_id;
        return $queryId;
    }
    public function save($data){
        $sql    =   "update ".$this->_table." set ";
        foreach($data as $key  =>  $v){
            $sets   =   $key." = '".$v."' ";
        }
        $sql    =   $sql.$sets;
        $data   =   $this->mysqli_query($sql);
    }
    public function delete(){
        $sql    =   "delete from ".$this->_table;
        $data   =   $this->mysqli_query($sql);
    }
    public function where($where){
        if(is_array($where)){
            foreach($where as $key  =>  $v){
                if(is_array($v)){
                    foreach($v as $k    => $u){
                        if($k=="not in"||$k=="in"){
                            $wheres =   $key." ".$k." ('".$u." ') ";
                        }else if($k ==  "like"){
                             $wheres =   $key." ".$k.' "%'.$u.'%" and ';
                        }else{
                            $wheres =   $key." ".$k." '".$u."' and ";
                        }
                    }
                    
                }else{
                    $wheres =   $key." = '".$v."' and ";
                }
            }
            $where =   $wheres;
        }
        $this->_where   =  " where " .$where;
        return $this;
    }
    public function limit($stat,$num){
        $this->_limit    = " limit ".$stat.",".$num;
        return $this;
    }
    public function order($order){
        $this->_order     =" order by ".$order;
        return $this;
    }
    public function data($data){
        $this->_data    =   $data;
        return $this;
    }
    public  function mysqli_query($sql ){
        $sql    .=   $this->_where.$this->_order.$this->_limit;
        $data   =   $this->_mysqli->query($sql);
        $this->_where   =   '';
        $this->_order   =   '';
        $this->_limit   =   '';
        return $data;
    }
    public function query($sql){
        $data   =   $this->_mysqli->query($sql);
        return $data;
    }
}
?>
